/* 函数组件,状态,状态之间相互影响 */
import React, { useReducer } from 'react'

export default function Reducer03() {
    const [count, dispatch] = useReducer((state, action) => {
        console.log("当前的值",state)
        console.log('dispatch触发的条件',action)
        switch (action) {
            case "add": 
                // 返回的是新值
                return state + 1;
            case "sup":
                return state - 1;
            default:
                return state;
        }
    }, 0)
    // 默认值是0
    return (
        <div>
            {count}
            <br />
            <button onClick={() => {dispatch('add') }}>相加2</button>
            <button onClick={() => {dispatch('sup') }}>相减2</button>
            <button onClick={() => {dispatch('*') }}>保持不变</button>
        </div>
    )
}
